﻿

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SDK.SqlSugar;
using SDK.SqlSugar.Configuration;
using SDK.SqlSugar.Configuration.Builders;
using SqlSugar;
using System.Diagnostics;

namespace Open.Core.Helpers.SqlSugar
{

    public static class SqlSugarHelper
    {
        public static IConfiguration configuration;


        public static void AddSqlSugar(this IServiceCollection serviceCollection, IConfigurationRoot _configuration)
        {
            configuration = _configuration;
        }

        public static SqlSugarClient Instance => GetClient();

        public static SqlSugarClient GetClient()
        {
            ISqlSugarSdkConfig config = new SqlSugarSdkConfigBuilder()
                .Enable(true)
                .SetDbType(DbType.MySql)
                .SetConnectionString(configuration.GetConnectionString("ConnectionString"))
                .SetInitKeyType(InitKeyType.Attribute)
                .WithAutoCloseConnection()
                .Build();

#if DEBUG || DEV||WDEBUG
            config.OnLogExecuting = (sql, pars) =>
            {
                Trace.WriteLine(sql + "\r\n");
            };
#endif
            SqlSugarSdkClient client = new SqlSugarSdkClient(config);

            return client.InitSqlSugarClient();
        }
    }
}
